import 'package:file_preview/file_preview.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:live_music/components/common/FilePreviewPage.dart';
import 'package:live_music/components/ui/tag.dart';
import 'package:live_music/components/ui/theme.dart';
import 'package:live_music/utils/cgq_utils.dart';

class FileCard extends StatefulWidget {
  FileCard({Key key}) : super(key: key);

  @override
  _FileCardState createState() => _FileCardState();
}

class _FileCardState extends State<FileCard>
    with SingleTickerProviderStateMixin {
  @override
  void initState() {
    super.initState();
    initTBS();
  }

  initTBS() async {
    var isInit = await FilePreview.tbsHasInit();
    if (!isInit) {
      await FilePreview.initTBS();
      return;
    }
  }

  String txt = "<text data-type='USER' style='color: #577C9F'>@你的名字</text> 撒大";
  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 0,
      margin: const EdgeInsets.only(bottom: 4),
      child: Padding(
        padding: const EdgeInsets.all(24),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              width: 44,
              height: 44,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(44),
                child: CgqUtils.imageCache(
                    'https://img2.baidu.com/it/u=747430821,2730080676&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750'),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '林允儿',
                      style: TextStyle(
                        color: WcaoTheme.base,
                        fontWeight: FontWeight.bold,
                        fontSize: WcaoTheme.fsL,
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(top: 4),
                      child: Text(
                        '2022-05-20',
                        style: TextStyle(
                          color: WcaoTheme.secondary,
                        ),
                      ),
                    ),
                    _buildFile(),
                    Container(
                      margin: const EdgeInsets.only(top: 12),
                      child: Text(
                        txt,
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(top: 12),
                      child: Wrap(
                          spacing: 12,
                          runSpacing: 6,
                          children: ['好看', '漂亮', '喜欢'].map((e) {
                            return Tag(
                              e,
                              borderRadius: BorderRadius.circular(24),
                              fontSize: WcaoTheme.fsBase,
                              padding: const EdgeInsets.symmetric(
                                vertical: 2,
                                horizontal: 8,
                              ),
                              color: WcaoTheme.primary,
                            );
                          }).toList()),
                    ),
                    Container(
                      margin: const EdgeInsets.only(top: 12),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          // 分享
                          iconText(Icons.offline_share, "9"),
                          Row(
                            children: [
                              // 关注
                              iconText(
                                Icons.favorite_border_outlined,
                                "7",
                              ),

                              // 评论
                              Container(
                                margin: const EdgeInsets.only(left: 24),
                                child: iconText(
                                  Icons.add_comment_outlined,
                                  "3",
                                ),
                              ),
                            ],
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _buildFile() {
    return Container(
      margin: const EdgeInsets.only(top: 8),
      padding: EdgeInsets.all(12),
      color: Colors.grey[100],
      child: ClipRRect(
          borderRadius: BorderRadius.circular(8),
          child: Row(children: [
            Container(
              margin: const EdgeInsets.only(right: 4),
              child: InkWell(
                  onTap: () async {
                    bool isInit = await FilePreview.tbsHasInit();
                    if (!isInit) {
                      print('object');
                      await FilePreview.initTBS();
                      return;
                    } else {
                      print('init');
                    }
                    Get.to(FilePreviewPage());
                  },
                  child: SizedBox(
                      width: 86,
                      height: 96,
                      child: ClipRRect(
                          borderRadius: BorderRadius.circular(4),
                          child: Image.asset('assets/images/icons/pdf.png')))),
            ),
            Expanded(
                flex: 1,
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        margin: const EdgeInsets.only(bottom: 24),
                        alignment: Alignment.centerLeft,
                        child: SizedBox(
                          child:
                              Text('标题哈啥十大还傻傻的和哈师大还傻傻的哈啥稍等哈岁的杀害大苏打啥的hash大厦哈啥',
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 2,
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Colors.grey[800],
                                    fontWeight: FontWeight.bold,
                                  )),
                        ),
                      ),
                      Container(
                          alignment: Alignment.centerRight,
                          margin: const EdgeInsets.only(bottom: 4, left: 12),
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                SizedBox(
                                    child: Text('2.5m',
                                        style: TextStyle(
                                          color: Colors.grey,
                                        )))
                              ])),
                    ]))
          ])),
    );
  }

  Row iconText(IconData icondata, String text) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Icon(
          icondata,
          color: WcaoTheme.secondary,
          size: WcaoTheme.fsXl,
        ),
        Padding(
          padding: const EdgeInsets.only(left: 2),
          child: Text(
            text,
            style: TextStyle(color: WcaoTheme.secondary),
          ),
        )
      ],
    );
  }
}
